﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetcodeTest._100._20
{
    public class Leetcode20
    {
        public void Test()
        {
            try
            {
                var a = IsValid("([)]");
                a = IsValid("()[]{}");
                a = IsValid("(]");
            }
            catch (Exception ex)
            { }
        }

        /*
         20. Valid Parentheses 

        Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
         */
        public bool IsValid(string s)
        {
            List<char> listChar = new List<char>();
            foreach (char a in s)
            {
                switch(a)
                {
                    case '(':
                    case '{':
                    case '[':
                        listChar.Add(a);
                        continue;
                }

                switch (a)
                {
                    case ')':
                        if (listChar.Count == 0 || listChar[listChar.Count - 1] != '(')
                            return false;
                        listChar.RemoveAt(listChar.Count - 1);
                        continue;
                    case '}':
                        if (listChar.Count == 0 || listChar[listChar.Count - 1] != '{')
                            return false;
                        listChar.RemoveAt(listChar.Count - 1);
                        continue;
                    case ']':
                        if (listChar.Count == 0 || listChar[listChar.Count - 1] != '[')
                            return false;
                        listChar.RemoveAt(listChar.Count - 1);
                        continue;
                }

            }
            if (listChar.Count != 0)
                return false;
            return true;
       
            
        }
        
    }
}
